﻿/*
 * @Author: wlingL
 * @Date: 2023-09-01 10:41:22
 * @LastEditTime: 2023-09-13 19:27:42
 * @FilePath: \MDKd:\file\stm32\sv_stu\Clicker\Driver\inc\ch452.h
 * @Description:
 *
 */

#ifndef __CH452_H
#define __CH452_H
#include "stm32f10x.h"
#define ORI_FUN
#define CH452_DRV
/**
 * add from csdn
 */
#ifdef CH452_DRV
/* CH451和CH452的常用命令码 */
#define CH452_NOP 0x0000      // 空操作
#define CH452_RESET 0x0201    // 复位
#define CH452_LEVEL 0x0100    // 加载光柱值,需另加7位数据
#define CH452_CLR_BIT 0x0180  // 段位清0,需另加6位数据
#define CH452_SET_BIT 0x01C0  // 段位置1,需另加6位数据
#define CH452_SLEEP 0x0202    // 进入睡眠状态
#define CH452_LEFTMOV 0x0300  // 设置移动方式-左移
#define CH452_LEFTCYC 0x0301  // 设置移动方式-左循环
#define CH452_RIGHTMOV 0x0302 // 设置移动方式-右移
#define CH452_RIGHTCYC 0x0303 // 设置移动方式-右循环
#define CH452_SELF_BCD 0x0380 // 自定义BCD码,需另加7位数据
#define CH452_SYSOFF 0x0400   // 关闭显示、关闭键盘
#define CH452_SYSON1 0x0401   // 开启显示
#define CH452_SYSON2 0x0403   // 开启显示、键盘
#define CH452_SYSON2W 0x0423  // 开启显示、键盘, 真正2线接口
#define CH452_NO_BCD 0x0500   // 设置默认显示方式,可另加3位扫描极限
#define CH452_BCD 0x0580      // 设置BCD译码方式,可另加3位扫描极限
#define CH452_TWINKLE 0x0600  // 设置闪烁控制,需另加8位数据
#define CH452_GET_KEY 0x0700  // 获取按键,返回按键代码
#define CH452_DIG0 0x0800     // 数码管位0显示,需另加8位数据
#define CH452_DIG1 0x0900     // 数码管位1显示,需另加8位数据
#define CH452_DIG2 0x0a00     // 数码管位2显示,需另加8位数据
#define CH452_DIG3 0x0b00     // 数码管位3显示,需另加8位数据
#define CH452_DIG4 0x0c00     // 数码管位4显示,需另加8位数据
#define CH452_DIG5 0x0d00     // 数码管位5显示,需另加8位数据
#define CH452_DIG6 0x0e00     // 数码管位6显示,需另加8位数据
#define CH452_DIG7 0x0f00     // 数码管位7显示,需另加8位数据

// BCD译码方式下的字符
// '0' - '9' 对应 0x00 - 0x09
#define CH452_BCD_SPACE 0x10 //' '
#define CH452_BCD_PLUS 0x11  //'+'
#define CH452_BCD_MINUS 0x12 //'-'
#define CH452_BCD_EQU 0x13   //'='
#define CH452_BCD_LEFT 0x14  //'['
#define CH452_BCD_RIGHT 0x15 //']'
#define CH452_BCD_UNDER 0x16 //'_'
#define CH452_BCD_CH_H 0x17  //'H'
#define CH452_BCD_CH_L 0x18  //'L'
#define CH452_BCD_CH_P 0x19  //'P'
#define CH452_BCD_DOT 0x1A   //'.'
#define CH452_BCD_SELF 0x1E  // 自定义
#define CH452_BCD_TEST 0x88  //'-'
#define CH452_BCD_DOT_X 0x80 //'-'

// csdn end
#endif

#ifdef KET_DEF
#define KEY_NULL 0
#define KEY_ESC 123
#define KEY_Yes 127
#define KEY_No 111
#define KEY_Left 95
#define KEY_Center 87
#define KEY_Right 79
#define KEY_Back 77
#define KEY_Up 86
#else
enum KEYCODE
{
    KEY_NULL,
    KEY_ESC = 123,
    KEY_Yes = 127,
    KEY_No = 111,
    KEY_Left = 95,
    KEY_Center = 87,
    KEY_Right = 79,
    KEY_Back = 77,
    KEY_Up = 86,

    // KEY_20MS = 20,
    // KEY_100MS = 100,
} ;
#endif

#define CH452_CLK RCC_APB2Periph_GPIOD
#define CH452_PORT GPIOD
#define CH452_DCLK_PIN GPIO_Pin_0
#define CH452_DIN_PIN GPIO_Pin_1
#define CH452_LOAD_PIN GPIO_Pin_2
#define CH452_DOUT_PIN GPIO_Pin_3

#define CH452_DIN_HIGH() GPIO_SetBits(CH452_PORT, CH452_DIN_PIN)
#define CH452_DIN_LOW() GPIO_ResetBits(CH452_PORT, CH452_DIN_PIN)
#define CH452_DCLK_UP()                                                                                                          \
    do                                                                                                                           \
    {                                                                                                                            \
        GPIO_ResetBits(CH452_PORT, CH452_DCLK_PIN);                                                                              \
        GPIO_SetBits(CH452_PORT, CH452_DCLK_PIN);                                                                                \
    } while (0)
#define CH452_DCLK_DOWN()                                                                                                        \
    do                                                                                                                           \
    {                                                                                                                            \
        GPIO_SetBits(CH452_PORT, CH452_DCLK_PIN);                                                                                \
        GPIO_ResetBits(CH452_PORT, CH452_DCLK_PIN);                                                                              \
    } while (0)

#define CH452_LOAD_UP()                                                                                                          \
    do                                                                                                                           \
    {                                                                                                                            \
        GPIO_ResetBits(CH452_PORT, CH452_LOAD_PIN);                                                                              \
        GPIO_SetBits(CH452_PORT, CH452_LOAD_PIN);                                                                                \
    } while (0)
#define CH452_LOAD_DOWN()                                                                                                        \
    do                                                                                                                           \
    {                                                                                                                            \
        GPIO_SetBits(CH452_PORT, CH452_LOAD_PIN);                                                                                \
        GPIO_ResetBits(CH452_PORT, CH452_LOAD_PIN);                                                                              \
    } while (0)

#ifdef CH452_MY_H
#define DIG_0 0x0800
#define DIG_1 0x0900
#define DIG_2 0x0A00
#define DIG_3 0x0B00
#define DIG_4 0x0C00
#define DIG_5 0x0D00
#define DIG_6 0x0E00
#define DIG_7 0x0F00

#define NUM_0 0x003F
#define NUM_1 0x003F
#define NUM_2 0x003F
#define NUM_3 0x003F
#define NUM_4 0x003F
#define NUM_5 0x003F
#define NUM_6 0x003F
#define NUM_7 0x003F
#define NUM_8 0x003F
#define NUM_9 0x003F
#define NUM_E 0x003F
#endif

void Ch452ASetCharacter(uint8_t byChannel, char cCh, u8 bDP);
void Ch452Init(void);
void Ch452_Write_Cmd(u16 cmd);
u8 KeyBoard_Hit(void);
int Get_Key(void);
u8 *Get_KeyStr(void);

extern u8 KeyCode;
#endif
